<?php
/**
 * Created by IntelliJ IDEA.
 * User: admin
 * Date: 4/13 0013
 * Time: 14:36
 */

namespace app\api\controller;


use think\Cache;
use think\Controller;

class Meituanheart extends Controller
{
    /**
     * Meituanheart constructor.
     * signkey 签名Key
     * developerid 开发者ID
     */
    public function __construct() {
        parent::__construct();
        define("SIGNKEY", 'r2wmwbjl2rmjftsh');
        define("DEVELOPERID","100435");
    }

    /**
     * 映射门店回掉地址
     * @return \think\response\Json
     */
    public function calback(){
        $map['epoiid'] = input('post.ePoiId');
        $map['token'] = input('post.appAuthToken');
        $map['shopid']=Cache::get(input('post.ePoiId'));//60分钟的缓存
        if(! $map['shopid']){
            return json(["data"=>"error"]);
        }
        $map['typeof']=2;
        $res = model('Meituan')->save($map);
        if($res){
            return json(["data"=>"success"]);
        }else{
            return json(["data"=>"error"]);
        }
    }

    /**
     * 映射团购门店回调地址
     * @return \think\response\Json
     */
    public function Tuanaddshopcalback(){
        $epoiid = request()->post('ePoiId');
        $map['tuantoken'] = request()->post('appAuthToken');
        $map['typeof'] = 1;
        $res = model('Meituan')->save($map,["epoiid"=>$epoiid]);
        if($res){
            return json(["data"=>"success"]);
        }else{
            return json(["data"=>"error"]);
        }
    }

    /**
     * 心跳
     */
    public function heart() {
        $url = urlencode("http://heartbeat.meituan.com/pos/heartbeat");
        //准备心跳上传data数据
        $shop=model("meituan")->field("epoiid")->select();
        foreach ($shop as $key => $value) {
            $list[]=$arr=['developerId'=>DEVELOPERID,'ePoiId'=>$value['epoiid'],"posId"=> $key,'time'=>time()];
        }
        foreach ($list as $key => $value) {
            $earr =  json_encode($value,JSON_PRETTY_PRINT );
            //签名
            $post_data = 'data='.$earr;
            $str = 'http://heartbeat.meituan.com/pos/heartbeat?'.$post_data;
            $sign = model("meituan")->SignKey($str);
            $post_data = $post_data."&sign=".$sign;
            //post发送数据
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL,'http://heartbeat.meituan.com/pos/heartbeat');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
            $output = curl_exec($ch);
            curl_close($ch);
            echo $output;//?
        }
    }

    /**
     * 云端心跳回调地址
     * @return \think\response\Json
     */
    public function heartcallback() {
        return json(["data"=>"OK"]);
    }

    /**
     * 总心跳
     */
    public function hearttotal(){
        $url="heartbeat.meituan.com/data/pos";
        $total=model("xcsstore")->where("storesou=2")->count();
        $shop=model("xcsstore")->where("storesou=2")->field(array('stationno','stationname','stationaddress','phone'))->select();
        foreach ($shop as $key => $value) {
            if(!$value['phone']){
                unset($shop[$key]);
            }else{
                $arr[]=['ePoiId'=>$value['stationno'],"posId"=> $key+1,'posType'=>3,'ePoiName'=>$value['stationname'],'erpVersion'=>'1.0','addr'=>$value['stationaddress'],'phone'=>$value['phone']];
            }
        }
        $data['list']=$arr;
        $data['total']=$total;
        $earr =  json_encode($data,JSON_PRETTY_PRINT );
        $post_data ="?developerId=".DEVELOPERID."&data=$earr";
        $sign_url=$url.$post_data;
        $sign= model("meituan")->SignKey($sign_url);
        $post_data = "sign=$sign&developerId=".DEVELOPERID."&data=$earr";
//        $post_data;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'heartbeat.meituan.com/data/pos');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        echo $output = curl_exec($ch);
        curl_close($ch);
    }

    /**
     * 订单回调地址
     * @return \think\response\Json
     */
    public function ordercallback(){
        $arr = json_decode(input('order'),true,512,JSON_BIGINT_AS_STRING);
//        file_put_contents(RUNTIME_PATH."log/mt/".time().".txt","get:".serialize(request()->get()).PHP_EOL."post:".serialize($arr));//记录日志
        $arrDetail =json_decode($arr['detail'],true,512,JSON_BIGINT_AS_STRING);
        $poiReceiveDetail = json_decode($arr['poiReceiveDetail'],true,512,JSON_BIGINT_AS_STRING);
        $actOrderChargeByMt = $poiReceiveDetail['actOrderChargeByMt'];
        $actOrderChargeByPoi =$poiReceiveDetail['actOrderChargeByPoi'];
        $extras = json_decode($arr['extras'],true,512,JSON_BIGINT_AS_STRING);
        $stationno = input('ePoiId'); //获取门店的编号
        $jd['orderstarttime'] = date('Y-m-d H:i:s',time()); //下单时间
        $jd['ordertotalmoney'] = $arr['total']*100; //订单总金额
        $jd['producestationno'] = $stationno; //门店编号
        $jd['buyerfullname'] = $arr['recipientName']; //收货人名称
        $jd['orderpaytype'] = ($arr['payType']==2?4:1); //订单支付类型(1.货到付款，4。在线支付)
        $jd['orderstatus'] = 41000; //订单状态 （31000  待付款 41000 待接单 32000 待出库 33040 配送中 33060 已完成）
        $jd['buyerfulladdress'] = $arr['recipientAddress']; //收货人地址
        $jd['buyermobile'] = $arr['recipientPhone']; //收货人手机号
        $jd['producestationnoisv'] = $stationno; // 商家门店编号
        $jd['orderbuyerremark'] = $arr['caution']; //收货人备注
        $jd['deliverystationno'] = $stationno; // 配送门店编号
        $jd['orderbuyerpayablemoney'] = $arr['total']*100; //客户实付金额
        $jd['platform'] = 2; //订单隶属 2   美团
        //$jd['shopid'] = 20000;
        $ddxh = model('Jdorder')->where(['producestationno' =>$stationno ])->where('orderstarttime like "' . date('Y-m-d') . '%"')->count();
        $ddxh = $ddxh + 1;
        $jd['ddxh'] = $ddxh; //（int）订单序号(生成方法为查询该店今日订单数量加1)
        $orderid=$jd['orderid'] = $arr['orderIdView']; //订单编号(其他平台的订单ID)
        $jd['meituanorderid'] = $arr['orderId'];

        //2016-12-29 添加美团配送方式分辨字段
        $jd['meituansendtype'] = $arr['logisticsCode'];//送货方式
//        file_put_contents("newOrder2.txt",$arr['logisticsCode']);
        //2016-12-29 添加美团配送方式分辨字段


        $jd['orderdiscountmoney'] = ($arr['originalPrice']-$arr['total'])*100;//订单优惠金额
        $jd['orderfreightmoney'] = ($arr['shippingFee']?$arr['shippingFee']*100:0);//订单运费金额
        $jd['packagingmoney'] = (isset($arrDetail['box_price'])?$arrDetail['box_price']*100:0);//包装费用
        $jd['costmoney'] = (isset($actOrderChargeByMt['moneyCent'])?$actOrderChargeByMt['moneyCent']:0);  //美团数据为分  不用*100    	平台承担金额
        $jd['salemoney'] = (isset($actOrderChargeByPoi['moneyCent'])?$actOrderChargeByPoi['moneyCent']:0);//美团数据为分  不用*100
        //商家承担金额
        $jd['need_invoice'] = ($arr['hasInvoiced']==0?2:1);
        $jd['discountname'] =isset($extras['remark']);
        $jd['discountdesc'] = isset($extras['remark']);
        $res = model('jdorder')->save($jd);//数据库主键 TODO
        $orderid= model('jdorder')->getLastInsID();
//        file_put_contents("newOrder3.txt",$res);
//        $mdid = model('xcsstore')->where(['stationno'=>$stationno])->column('customid')[0];
        if ($res) {
            $goodsarr = $arrDetail;
            foreach ($goodsarr as $k => $v) {
                $jddetail['pid'] = $orderid;
                $jddetail['skuid'] = $v['app_food_code'];
                $jddetail['skuname'] = $v['food_name'];
                $jddetail['skujdprice'] = $v['price'] * $v['food_discount']*100;
                $jddetail['jiage'] = $v['price'] * $v['quantity'] * 100;
                $jddetail['skucount'] = $v['quantity'];
                $jddetail['spid'] = $stationno; //生成订单
                model('jdorderdetail')->save($jddetail);
            }
        }
        return json(["data"=>"OK"]);
    }

    /**
     * 美团推送取消订单
     * @return \think\response\Json
     */
    public function delordercallback(){
        if(request()->post()){
            $jdorder=model('jdorder');
            $orderCancel=input('orderCancel');
            $orderCancel = json_decode($orderCancel,true,512,JSON_BIGINT_AS_STRING);
            $orderId=$orderCancel['orderId'];
            $data['orderstatus']='20020';
            $jdorder->save($data,["meituanorderid"=>$orderId]);
            $text2=$jdorder->getlastsql();
        }
        return json(["data"=>"OK"]);
    }

    /**
     * 美团推送确认订单返回消息
     * @return \think\response\Json
     */
    public function querenordercallback(){
        if(request()->post()){
            $jdorder=model('jdorder');
            $orderCancel=input('order');
            $orderCancel = json_decode($orderCancel,true,512,JSON_BIGINT_AS_STRING);
            $orderId=$orderCancel['orderId'];
            $data['orderstatus']='32000';
            $check=$jdorder->save($data,["meituanorderid"=>$orderId]);
            //订单确认发送美团自送数据更换状态
            if($check){
                $produceStationNo=$jdorder->where("meituanorderid='$orderId'")->column("producestationno")[0];
                $meituan=model("meituan");
                $token=$meituan->where("epoiid='$produceStationNo'")->field("token")->find();
                $token=$token['token'];
                $shopid=$produceStationNo;
//                $orderId=$orderId;
                $checksend=$this->selfend($orderId,$shopid,$token);
                if($checksend){
                    $data['orderstatus']='33040';
                    $check=$jdorder->save($data,["meituanorderid"=>$orderId]);
                }
            }
        }
        return json(["data"=>"OK"]);
    }

    /**
     * 美团推送用户退已接单的订单返回数据
     * @return \think\response\Json
     */
    public function drawcallback(){
        if(request()->post()){
            $jdorder=model('jdorder');
            $orderCancel=input('orderRefund');
            $orderCancel = json_decode($orderCancel,true,512,JSON_BIGINT_AS_STRING);
            $orderId=$orderCancel['orderId'];
            $data['refundstatus']='2';
            $jdorder->save($data,["meituanorderid"=>$orderId]);
        }
        return json(["data"=>"OK"]);
    }

    /**
     * 美团推送已完成返回数据
     * @return \think\response\Json
     */
    public function finishcallback(){
        if(request()->post()){
            $jdorder=model('jdorder');
            $orderCancel=input('order');
            $orderCancel = json_decode($orderCancel,true,512,JSON_BIGINT_AS_STRING);
            $orderId=$orderCancel['orderId'];
            $data['orderstatus']='33060';
            $jdorder->save($data,["meituanorderid"=>$orderId]);
            $text2=$jdorder->getlastsql();
        }
        return json(["data"=>"OK"]);
    }

    /**
     * post请求
     * @param $url
     * @param $post_data
     * @return mixed
     */
    public function post($url,$post_data){
        $postdata = http_build_query($post_data);
        $options = array(
            'http' => array(
                'method' => 'POST',
                'header' => 'Content-type:application/x-www-form-urlencoded',
                'content' => $postdata,
                'timeout' => 15 * 60
            )
        );
        $context = stream_context_create($options);
        $result = file_get_contents($url, false, $context);
//        file_put_contents('newOrder.txt',$result);
        $result=json_decode($result,true,512,JSON_BIGINT_AS_STRING);
        $data=$result['data'];
        return $data;
    }

    /**
     * 向美团发送取消订单消息
     * @param $orderId
     * @param $shopid
     * @param $token
     * @return bool
     */
    public function quxiaomeituan($orderId,$shopid,$token){
        $url ="http://api.open.cater.meituan.com/waimai/order/cancel";
        $timestamp=time();
        $reason="1";
        $str =$url."?timestamp=$timestamp&appAuthToken=$token&charset=utf-8&orderId=$orderId&reasonCode=1204&reason=$reason";
        $sign =  model("meituan")->SignKey($str);
        $post_data = array(
            'timestamp'=>$timestamp,
            'appAuthToken'=>$token,
            'charset'=>'utf-8',
            'orderId'=>$orderId,
            'reasonCode'=>'1204',
            'reason'=>$reason,
            'sign'=>$sign
        );
        $data=$this->post($url,$post_data);
        if($data=="ok"){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 向美团发送确认订单消息
     * @param $orderId
     * @param $shopid
     * @param $token
     * @return bool
     */
    public function confirmodel($orderId,$shopid,$token){
        $url ="http://api.open.cater.meituan.com/waimai/order/confirm";
        $timestamp=time();
        $str =$url."?timestamp=$timestamp&appAuthToken=$token&charset=utf-8&orderId=$orderId";
        $sign =  model("meituan")->SignKey($str);
        $post_data = array(
            'timestamp'=>$timestamp,
            'appAuthToken'=>$token,
            'charset'=>'utf-8',
            'orderId'=>$orderId,
            'sign'=>$sign
        );
        $data=$this->post($url,$post_data);
        if($data=="ok"){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 向美团发送自送消息
     * @param $orderId
     * @param $shopid
     * @param $token
     * @return bool
     */
    public function selfend($orderId,$shopid,$token){
        //2016-12-29  添加专送方式
        $meituansendtype=model("jdorder")->where("meituanorderid='$orderId'")->column("meituansendtype")[0];
//        file_put_contents("newOrder6.txt",$meituansendtype);
        if($meituansendtype=="1001" || $meituansendtype=="1002" || $meituansendtype=="1004"){

            $url ="http://api.open.cater.meituan.com/waimai/order/dispatchShip";//2016-12-29 专送地址

        }else{
//            file_put_contents("newOrder6.txt","自送");
            $url ="http://api.open.cater.meituan.com/waimai/order/delivering";//2016-12-29 自送地址
        }


        //$url ="http://api.open.cater.meituan.com/waimai/order/delivering";2016-12-29原始数据
        $timestamp=time();
        $str =$url."?timestamp=$timestamp&appAuthToken=$token&charset=utf-8&orderId=$orderId";
        $sign =  model("meituan")->SignKey($str);
        $post_data = array(
            'timestamp'=>$timestamp,
            'appAuthToken'=>$token,
            'charset'=>'utf-8',
            'orderId'=>$orderId,
            'sign'=>$sign
        );
        $data=$this->post($url,$post_data);
        if($data=="ok"){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 向美团发完成自送消息
     * @param $orderId
     * @param $shopid
     * @param $token
     * @return bool
     */
    public function querenselfend($orderId,$shopid,$token){
        $url ="http://api.open.cater.meituan.com/waimai/order/delivered";
        $timestamp=time();
        $str =$url."?timestamp=$timestamp&appAuthToken=$token&charset=utf-8&orderId=$orderId";
        $sign =  model("meituan")->SignKey($str);
        $post_data = array(
            'timestamp'=>$timestamp,
            'appAuthToken'=>$token,
            'charset'=>'utf-8',
            'orderId'=>$orderId,
            'sign'=>$sign
        );
        $data=$this->post($url,$post_data);
        if($data=="ok"){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 向美团发送同意退款消息
     * @param $orderId
     * @param $reason
     * @param $token
     * @return bool
     */
    public function agreeRefund($orderId,$reason,$token){
//        file_put_contents("newOrder1.txt",$orderId.$reason.$token);
        $url ="http://api.open.cater.meituan.com/waimai/order/agreeRefund";
        $timestamp=time();
        //$reason="1";
        $str1 ="timestamp=$timestamp&appAuthToken=$token&charset=utf-8&orderId=$orderId&reason=$reason";
        $str=$url."?".$str1;
        $sign =  model("meituan")->SignKey($str);
        //验证提交乱码  已解决添加heard
        $ch = curl_init();
        $this_header = array(
            "content-type: application/x-www-form-urlencoded; 
		charset=UTF-8"
        );
        curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $str1."&sign=$sign");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $result = curl_exec($ch);
//        file_put_contents("newOrder5.txt",$result);
        $data = json_decode($result,true,512,JSON_BIGINT_AS_STRING);
        if($data['data']){
            return true;
            //echo '{"status":1,"info":"请求成功"}';
        }else{
            return false;
            //echo '{"status":0,"info":"请求错误"}';
        }
    }

    /**
     * 向美团发送拒绝退款消息
     * @param $orderId
     * @param $shopid
     * @param $token
     * @return bool
     */
    public function disagreeRefund($orderId,$shopid,$token){
        $url ="http://api.open.cater.meituan.com/waimai/order/rejectRefund";
        $timestamp=time();
        $reason="1";
        $str =$url."?timestamp=$timestamp&appAuthToken=$token&charset=utf-8&orderId=$orderId&reason=$reason";
        $sign =  model("meituan")->SignKey($str);
        $post_data = array(
            'timestamp'=>$timestamp,
            'appAuthToken'=>$token,
            'charset'=>'utf-8',
            'orderId'=>$orderId,
            'sign'=>$sign,
            'reason'=>$reason
        );
        $data=$this->post($url,$post_data);
        if($data=="ok"){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据门店查询菜品信息
     * @return string
     */
    public function queryBaseListByEPoiId() {
        $res = $this->checkUser(input('token'));
        if($res){
            return json_encode('用户不存在');
        }
        $id = input('id');
        $url = 'http://api.open.cater.meituan.com/waimai/dish/queryListByEPoiId';
        $time = time();
        $info = model('Meituan')->where(['epoiid'=>$id])->column('token')[0];
        $data = [
            'timestamp'=>$time,
            'appAuthToken' =>$info,
            'charset'=>'utf-8',
            'ePoiId'=> $id,
            'offset' =>0,
            'limit'=>100
        ];
        $str = $url.'?'.http_build_query($data);
        $data['sign'] = model("meituan")->SignKey($str);
        $res = model("meituan")->method($data,$url,'get');
        $info = json_decode($res,true,512,JSON_BIGINT_AS_STRING);
        if($info['data']){
            foreach ($info['data'] as $v=>$k){
                unset($info['data'][$v]['skus']);
            }
            return json_encode($info);
        }else{
            return json_encode('error');
        }
    }

    /**
     *  获取商品类型
     *  @param token用来获取用户id判断是否存在
     *  @param  epoiid门店id用来获取数据
     *  @return string|\think\response\Json
     */
    public function getType(){
        $userid=self::verifytoken(request()->post("token"));
        if (empty($userid)) {
            return json(["status"=>0,"info"=>"用户错误"]);
        }else{
            $epoiid=input("post.epoiid");
            $meituantype=model("meituantype");
            $res=$meituantype->field(array("name","sequence"))->where("epoid='$epoiid'")->select();
            if($res){
                $data=json_encode($res);
                return $data;
            }else{
                return json(["status"=>0,"info"=>"请求错误"]);
            }
        }
    }

    /**
     * 查询用户所在门店的开业状态
     * @param token用来获取用户id判断是否存在
     * @param epoiid门店id用来获取数据
     * @return \think\response\Json
     */
    public function checkmeituan(){
        $userid=self::verifytoken(request()->post("token"));
        if (empty($userid)) {
            return json(["status"=>0,"info"=>"请求错误"]);
        }else{
            $this->gengxin();
            $epoiid=input("post.epoiid");//获取返回来的门店id
            $meituan=model("meituan");
            $xcsstore=model("xcsstore");
            $yn=$xcsstore->where("stationno='$epoiid'")->column("yn")[0];
            if($yn=="1"){
                return json(["status"=>1,"info"=>"正在营业中"]);
            }else{
                return json(["status"=>2,"info"=>"已歇业"]);
            }
        }
    }

    /**
     * @param $token
     * @return bool
     */
    public function checkUser($token) {
        $userid=self::verifytoken(request()->post("token"));
        if(empty($userid)){
            return true;
        }else{
            return false;
        }
    }

    /**
     *
     */
    public function gengxin(){
        $Meituansql = model('Meituan');
        $meituan=$Meituansql->select();
        foreach ($meituan as $key => $value) {
            $ePoiId=$value['epoiid'];
            $token=$value['token'];
            $time=time();
            $url="http://api.open.cater.meituan.com/waimai/poi/queryPoiInfo?timestamp=$time&appAuthToken=$token&charset=UTF-8&ePoiIds=$ePoiId";
            $signKey= model("meituan")->SignKey($url);
            $url=$url."&sign=".$signKey;
            $json=curl_get($url);
            $data[]=json_decode($json,true,512,JSON_BIGINT_AS_STRING);
        }
        foreach ($data as $key => $value) {
            if(isset($value['error'])){
                unset($data[$key]);//清除错误门店信息
            }
        }
        $xcsstore = model('xcsstore');
        foreach ($data as $k => $value) {
            foreach ($value as $vals) {
                foreach ($vals as $key => $val) {
                    if($val['isOpen']=='1'){}else{$val['isOpen']='2';}
                    $stationno=$val['ePoiId'];
                    $sqlres=array(
                        "stationno"=>$stationno,        //门店编号
                        "stationname"=>$val['name'],            //门店名称
                        "stationaddress"=>$val['address'],  //门店地址
                        "contacts"=>$val['name'],           //联系人
                        "phone"=>$val['phone'],             //联系电话
                        "storeact"=>$val['noticeInfo'],     //门店活动
                        "yn"=>$val['isOpen'],               //运营状态
                        "storesou"=>'2',                    //分类->美团2
                        "createtime"=>time(),               //最后更新时间
                    );
                    $res=$check=$xcsstore->where("stationno='$stationno'")->count();
                    if($check){
                        $res=$xcsstore->save($sqlres,["stationno"=>$stationno]);
                    }else{
                        $res=$xcsstore->save($sqlres);//TODO
                    }
                }
            }
        }
    }

    /**
     * @return false|\PDOStatement|string|\think\Collection|\think\response\Json
     */
    public function meituanHouse(){
        $userid=self::verifytoken(request()->post("token"));
        if (empty($userid)) {
            return json(["status"=>0,"info"=>"请求错误"]);
        }else{
            $data=model('xcsstore')->field(array("stationno","stationname"))->where("customid='$userid' and storesou='2'")->select();
            $data=json_encode($data);
            if($data){
                return $data;
            }else{
                return json(["status"=>0,"info"=>"请求错误"]);
            }
        }
    }

    /**
     * 向美团发送取消订单消息
     * 2016-12-26中文原因乱码已解决
     * @param $orderId
     * @param $epoiid
     * @param int $reason
     * @return bool
     */
    public function meituanquxiao($orderId,$epoiid,$reason=1){

        $token=model("meituan")->where("epoiid='$epoiid'")->column('token')[0];
        $reason=1;
        $url ="http://api.open.cater.meituan.com/waimai/order/cancel";
        $timestamp=time();
        $post_data = array(
            'timestamp'=>$timestamp,
            'appAuthToken'=>$token,
            'charset'=>'utf-8',
            'orderId'=>$orderId,
            'reasonCode'=>'2007'
        );
        $str  = http_build_query($post_data)."&reason=$reason";

        $sign = $this->SignKey($str);
        $str= $str."&sign=$sign";
//        file_put_contents("newOrder4.txt",$str);
//验证提交乱码  已解决添加heard
        $ch = curl_init();
        $this_header = array(
            "content-type: application/x-www-form-urlencoded; 
charset=UTF-8"
        );
        curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        $result = curl_exec($ch);
//        file_put_contents("newOrder5.txt",$result);
        $data = json_decode($result,true,512,JSON_BIGINT_AS_STRING);
        if($data['data']){
            return true;
            //echo '{"status":1,"info":"请求成功"}';
        }else{
            return false;
            //echo '{"status":0,"info":"请求错误"}';
        }
    }

    /**
     * @param $url
     * @return string
     * 中文乱码不是签名问题已验证
     */
    public function  SignKey($url){
        $signKey=SIGNKEY;
        $urlarr=explode("&", $url);
        foreach ($urlarr as $key => $value) {
            $urlarr[$key] = explode("=", $value);
            if($urlarr[$key][1]==""){
                unset($urlarr[$key]);//删除空参
            }else{
                $urlarr[$key]=implode($urlarr[$key],"");
            }
        }
        asort($urlarr);//重新排序
        $sring=$signKey.implode($urlarr,"");
        return sha1($sring);//获得签名
    }


    /**
     * 闪惠订单通知
     */
    public function shcallback(){
        file_put_contents(RUNTIME_PATH."log/sh/".time().".txt","get:".serialize(request()->get()).PHP_EOL."post:".serialize(request()->post()));//记录日志
        return json(["data"=>"OK"]);
    }

}